<template>
  <div>

    <!--搜索-->
    <div style="margin: 10px 0">
      <el-input v-model="username" placeholder="请输入名称" style="width: 200px" suffix-icon="el-icon-search"
                clearable></el-input>
      <el-input v-model="email" placeholder="请输入邮箱" style="width: 200px" suffix-icon="el-icon-message"
                clearable class="ml-5"></el-input>
      <el-input v-model="address" placeholder="请输入地址" style="width: 200px" suffix-icon="el-icon-position"
                clearable class="ml-5"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <!--编辑-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm
          class="ml-5 mr-5"
          confirm-button-text='我已经下定了决心'
          cancel-button-text='不，不要。不要啊'
          icon="el-icon-info"
          icon-color="red"
          title="这可是一堆数据哥们，好好思考一下？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
      <el-upload style="display: inline-block" :show-file-list="false" accept="xlsx"
                 :on-success="handleExcelImportSuccess"
                 action="http://localhost:9090/user/import">
        <el-button type="primary">导入 <i class="el-icon-bottom"></i></el-button>
      </el-upload>
      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
    </div>
    <!--          表格数据-->
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column label="ID" prop="id" width="80"></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="角色" prop="role">
        <template v-slot="scope">
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_ADMIN'">管理员</el-tag>
          <el-tag type="warning" v-if="scope.row.role === 'ROLE_TEACHER'">老师</el-tag>
          <el-tag type="success" v-if="scope.row.role === 'ROLE_STUDENT'">学生</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="昵称" prop="nickname"></el-table-column>
      <el-table-column label="邮箱" prop="email"></el-table-column>
      <el-table-column label="电话" prop="phone"></el-table-column>
      <el-table-column label="地址" prop="address"></el-table-column>
      <el-table-column label="操作" prop="handle" width="400">
        <template #default="scope">
          <el-button type="primary" @click="lookCourse(scope.row.courses)" v-if="scope.row.role === 'ROLE_TEACHER'">
            查看教授课程 <i class="el-icon-view"></i>
          </el-button>
          <el-button type="warning" @click="lookStuCourse(scope.row.id)"
                     v-if="scope.row.role === 'ROLE_STUDENT'">
            查看已选课程 <i class="el-icon-view"></i>
          </el-button>
          <el-button type="success" @click="handEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确认删除'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="Are you Sure？"
              @confirm="handDelete(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-delete"></i>
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <!--          分页-->
    <div class="demo-pagination-block" style="padding: 10px 0">
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :page-sizes="[5, 10, 15, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="form" label-width="80px" size="small">
          <el-form-item label="用户名">
            <el-input v-model="form.username"  autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色">
            <el-select clearable v-model="form.role" placeholder="请选择角色" style="width: 100%">
              <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="课程信息" :visible.sync="CourseVis" width="30%">
        <el-table :data="courses">
          <el-table-column prop="name" label="课程名称"></el-table-column>
          <el-table-column prop="score" label="学分"></el-table-column>
        </el-table>
      </el-dialog>

      <el-dialog title="选课信息" :visible.sync="StudentVis" width="30%">
        <el-table :data="stuCourses">
          <el-table-column prop="name" label="课程名称"></el-table-column>
          <el-table-column prop="score" label="学分"></el-table-column>
        </el-table>
      </el-dialog>
    </div>
    <!--          弹窗-->
  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      username: '',
      email: '',
      address: '',
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      roles: [],
      courses: [],
      stuCourses: [],
      CourseVis: false,
      StudentVis: false
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.request.get("/user/page", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          username: this.username,
          email: this.email,
          address: this.address
        }
      }).then(res => {
        // console.log(res);
        this.tableData = res.data.records
        this.total = res.data.total
      })

      this.request.get("/role").then((res) => {
        this.roles = res.data
      })
    },
    reset() {
      this.username = ''
      this.email = ''
      this.address = ''
      this.load()

    },
    handAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {
      this.request.post("/user", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false;
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handEdit(row) {
      this.form = row
      this.dialogFormVisible = true
      this.form = Object.assign({}, row) //点击取消后不保存数据 其实就是拷贝到一个空对象，就得到了原始对象的克隆，不进行修改，返回原来的对象
    },
    handDelete(id) {
      this.request.delete("/user/" + id).then((res) => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val); //val是一个对象
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(value => value.id) // [{},{}] => [1,2...]
      this.request.post("/user/del/batch", ids).then((res) => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.currentPage = pageNum
      this.load()
    },
    exp() {
      window.open("http://localhost:9090/user/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    },
    lookCourse(courses) {
      this.courses = courses
      this.CourseVis = true
    },
    lookStuCourse(id) {
      this.request.post("/user/stuCourse").then((res) => {
        console.log(res)
        console.log(res.data.length)
        // console.log(res.data.stuCourses)

          if (res.data[0].id === id){
            this.stuCourses = res.data[0].stuCourses
          }else if (res.data[1].id === id){
            this.stuCourses = res.data[1].stuCourses
          }else {
            this.stuCourses = []

          }
      })
      this.StudentVis = true
      this.stuCourses = []
    }
  }
}
</script>

<style scoped>

</style>